<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML/CSS预览与导出工具</title>
    <script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #1a2a6c, #b21f1f, #1a2a6c);
            color: #333;
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1400px;
            margin: 0 auto;
        }
        
        header {
            text-align: center;
            padding: 30px 0;
            color: white;
            text-shadow: 0 2px 4px rgba(0,0,0,0.3);
        }
        
        header h1 {
            font-size: 2.8rem;
            margin-bottom: 10px;
        }
        
        header p {
            font-size: 1.2rem;
            max-width: 700px;
            margin: 0 auto;
            opacity: 0.9;
        }
        
        .app-container {
            display: flex;
            flex-wrap: wrap;
            gap: 25px;
            margin-top: 20px;
            height: 80vh;
        }
        
        .editor-panel, .preview-panel {
            flex: 1;
            min-width: 300px;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            overflow: hidden;
            display: flex;
            flex-direction: column;
        }
        
        .panel-header {
            background: #2c3e50;
            color: white;
            padding: 15px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .panel-title {
            font-size: 1.4rem;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .panel-actions {
            display: flex;
            gap: 10px;
        }
        
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 50px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        
        .btn-primary {
            background: #3498db;
            color: white;
        }
        
        .btn-primary:hover {
            background: #2980b9;
            transform: translateY(-2px);
            box-shadow: 0 6px 8px rgba(0,0,0,0.15);
        }
        
        .btn-success {
            background: #2ecc71;
            color: white;
        }
        
        .btn-success:hover {
            background: #27ae60;
            transform: translateY(-2px);
            box-shadow: 0 6px 8px rgba(0,0,0,0.15);
        }
        
        .code-container {
            display: flex;
            flex-direction: column;
            gap: 20px;
            padding: 20px;
            flex: 1;
        }
        
        .code-section {
            display: flex;
            flex-direction: column;
            flex: 1;
        }
        
        .code-label {
            font-weight: 600;
            margin-bottom: 10px;
            color: #2c3e50;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        textarea {
            flex: 1;
            padding: 15px;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            font-family: 'Consolas', 'Courier New', monospace;
            font-size: 0.95rem;
            resize: none;
            transition: border-color 0.3s;
        }
        
        textarea:focus {
            outline: none;
            border-color: #3498db;
            box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
        }
        
        .preview-content {
            flex: 1;
            padding: 20px;
            overflow: auto;
            border: 1px solid #eee;
            background: white;
            position: relative;
        }
        
        .preview-frame {
            width: 100%;
            height: 100%;
            border: none;
        }
        
        .export-overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.7);
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            color: white;
            z-index: 100;
            opacity: 0;
            pointer-events: none;
            transition: opacity 0.3s;
        }
        
        .export-overlay.active {
            opacity: 1;
            pointer-events: all;
        }
        
        .spinner {
            width: 50px;
            height: 50px;
            border: 5px solid rgba(255,255,255,0.3);
            border-radius: 50%;
            border-top-color: white;
            animation: spin 1s linear infinite;
            margin-bottom: 20px;
        }
        
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        
        footer {
            text-align: center;
            padding: 30px 0;
            color: rgba(255,255,255,0.7);
            font-size: 0.9rem;
            margin-top: 20px;
        }
        
        .features {
            display: flex;
            justify-content: center;
            gap: 30px;
            flex-wrap: wrap;
            margin: 30px 0;
        }
        
        .feature {
            background: rgba(255,255,255,0.1);
            backdrop-filter: blur(10px);
            border-radius: 10px;
            padding: 20px;
            width: 250px;
            color: white;
            text-align: center;
        }
        
        .feature i {
            font-size: 2.5rem;
            margin-bottom: 15px;
            color: #3498db;
        }
        
        .feature h3 {
            margin-bottom: 10px;
            font-size: 1.3rem;
        }
        
        @media (max-width: 768px) {
            .app-container {
                flex-direction: column;
                height: 160vh;
            }
            
            header h1 {
                font-size: 2rem;
            }
            
            .panel-title {
                font-size: 1.2rem;
            }
            
            .btn {
                padding: 8px 15px;
                font-size: 0.9rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1><i class="fas fa-code"></i> HTML/CSS 预览与导出工具</h1>
            <p>实时编辑HTML和CSS代码，预览效果并导出为图片</p>
            
            <div class="features">
                <div class="feature">
                    <i class="fas fa-laptop-code"></i>
                    <h3>实时预览</h3>
                    <p>即时查看代码效果，所见即所得</p>
                </div>
                <div class="feature">
                    <i class="fas fa-file-image"></i>
                    <h3>导出图片</h3>
                    <p>一键将预览效果保存为PNG图片</p>
                </div>
                <div class="feature">
                    <i class="fas fa-mobile-alt"></i>
                    <h3>响应式设计</h3>
                    <p>在手机、平板和电脑上均可使用</p>
                </div>
            </div>
        </header>
        
        <div class="app-container">
            <div class="editor-panel">
                <div class="panel-header">
                    <div class="panel-title">
                        <i class="fas fa-edit"></i> 代码编辑器
                    </div>
                    <div class="panel-actions">
                        <button id="saveBtn" class="btn btn-primary">
                            <i class="fas fa-save"></i> 保存并预览
                        </button>
                    </div>
                </div>
                <div class="code-container">
                    <div class="code-section">
                        <div class="code-label">
                            <i class="fab fa-html5"></i> HTML 代码
                        </div>
                        <textarea id="htmlCode" placeholder="在此输入HTML代码...">&lt;div class="card" id="root"&gt;
  &lt;div class="card-header"&gt;
    &lt;h2&gt;欢迎使用预览工具&lt;/h2&gt;
  &lt;/div&gt;
  &lt;div class="card-body"&gt;
    &lt;p&gt;这是一个示例卡片，编辑左侧代码查看实时效果！&lt;/p&gt;
    &lt;button class="btn"&gt;示例按钮&lt;/button&gt;
  &lt;/div&gt;
&lt;/div&gt;</textarea>
                    </div>
                    <div class="code-section">
                        <div class="code-label">
                            <i class="fab fa-css3-alt"></i> CSS 代码
                        </div>
                        <textarea id="cssCode" placeholder="在此输入CSS代码...">.card {
  border-radius: 10px;
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
  overflow: hidden;
  max-width: 500px;
  margin: 0 auto;
  font-family: Arial, sans-serif;
}

.card-header {
  background: #3498db;
  color: white;
  padding: 20px;
  text-align: center;
}

.card-header h2 {
  margin: 0;
}

.card-body {
  padding: 25px;
  background: white;
  text-align: center;
}

.card-body p {
  color: #555;
  line-height: 1.6;
  margin-bottom: 20px;
}

.btn {
  background: #2ecc71;
  color: white;
  border: none;
  padding: 12px 25px;
  border-radius: 50px;
  cursor: pointer;
  font-size: 1rem;
  transition: all 0.3s;
}

.btn:hover {
  background: #27ae60;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}</textarea>
                    </div>
                </div>
            </div>
            
            <div class="preview-panel">
                <div class="panel-header">
                    <div class="panel-title">
                        <i class="fas fa-eye"></i> 预览效果
                    </div>
                    <div class="panel-actions">
                        <button id="exportBtn" class="btn btn-success">
                            <i class="fas fa-download"></i> 导出为图片
                        </button>
                    </div>
                </div>
                <div class="preview-content">
                    <iframe id="previewFrame" class="preview-frame"></iframe>
                    <div id="exportOverlay" class="export-overlay">
                        <div class="spinner"></div>
                        <h3>正在生成图片，请稍候...</h3>
                    </div>
                </div>
            </div>
        </div>
        
        <footer>
            <p>© 2023 HTML/CSS预览与导出工具 | 设计：前端开发工程师</p>
        </footer>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const htmlCode = document.getElementById('htmlCode');
            const cssCode = document.getElementById('cssCode');
            const previewFrame = document.getElementById('previewFrame');
            const saveBtn = document.getElementById('saveBtn');
            const exportBtn = document.getElementById('exportBtn');
            const exportOverlay = document.getElementById('exportOverlay');
            
            // 初始化预览
            updatePreview();
            
            // 保存并预览按钮事件
            saveBtn.addEventListener('click', updatePreview);
            
            // 导出为图片按钮事件
            exportBtn.addEventListener('click', exportAsImage);
            
            // 更新预览
            function updatePreview() {
                const html = htmlCode.value;
                const css = cssCode.value;
                
                const fullHtml = `
                    <!DOCTYPE html>
                    <html>
                    <head>
                        <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
                            ${css}</style>
                        <meta name="viewport" content="width=device-width, initial-scale=1.0">
                    </head>
                    <body>
                        ${html}
                    </body>
                    </html>
                `;
                
                // 将完整HTML写入iframe
                previewFrame.srcdoc = fullHtml;
            }
            
            // 导出为图片
            function exportAsImage() {
                // 显示加载指示器
                exportOverlay.classList.add('active');
                
                // 等待iframe内容加载完成
                previewFrame.addEventListener('load', function onLoad() {
                    previewFrame.removeEventListener('load', onLoad);
                    
                    // 使用html2canvas将iframe内容转换为图片
                    html2canvas(previewFrame.contentWindow.document.getElementById("root"), {
                        scale: 2, // 提高导出图片质量
                        useCORS: true,
                        allowTaint: true,
                        logging: false
                    }).then(canvas => {
                        // 创建下载链接
                        const link = document.createElement('a');
                        link.download = 'preview-export.png';
                        link.href = canvas.toDataURL('image/png');
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                        
                        // 隐藏加载指示器
                        exportOverlay.classList.remove('active');
                    }).catch(error => {
                        console.error('导出图片失败:', error);
                        alert('导出失败，请查看控制台获取详细信息');
                        exportOverlay.classList.remove('active');
                    });
                }, { once: true });
                
                // 确保iframe内容是最新的
                updatePreview();
            }
        });
    </script>
</body>
</html>